package com.xuantu.pattern.create.singleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableSingletonTest {

	public static void main(String[] args) throws Exception {
		SerializableSingleton instance1 = SerializableSingleton.getInstance();
		SerializableSingleton instance2 = SerializableSingleton.getInstance();
		System.out.println(instance1);
		System.out.println(instance2);
		
		//序列化、反序列化
		FileOutputStream fos = new FileOutputStream("object.out");  
		ObjectOutputStream oos = new ObjectOutputStream(fos);  
		oos.writeObject(instance1);
		oos.close();
        fos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.out"));  
        SerializableSingleton instance11 = (SerializableSingleton)ois.readObject();   
        System.out.println(instance1);
        System.out.println(instance11);
		
		
		HungrySingleton instance3 = HungrySingleton.getInstance();
		fos = new FileOutputStream("object.out");  
		oos = new ObjectOutputStream(fos);  
        oos.writeObject(instance3);  
        oos.close();  
        fos.close();
        ois = new ObjectInputStream(new FileInputStream("object.out"));  
        HungrySingleton instance4 = (HungrySingleton)ois.readObject();        
        System.out.println(instance3);
        System.out.println(instance4);
        
		
	}
	
}
